package com.javabase.y2021.m12.day1224;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2021/12/24 下午 03:59
 * @version: 1.0
 */
public class DownUtil {
    // 定义下载资源的路径
    private String path;

    private String targetFile;
    private int threadNum;

    private DownThread[] threads;
    private int fileSize;

    public DownUtil(String path, String targetFile, int threadNum){
        this.path = path;
        this.threadNum = threadNum;
        this.targetFile = targetFile;
        threads = new DownThread[threadNum];
    }

    public void download() throws Exception{
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept","*/*");
        conn.setRequestProperty("Charset","UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");
        // 得到文件大小
        fileSize = conn.getContentLength();
        conn.disconnect();
        int currentPartSize = fileSize / threadNum + 1;
        RandomAccessFile file = new RandomAccessFile(targetFile, "rw");

        file.setLength(fileSize);
        file.close();

        for(int i = 0; i < threadNum; i++){
            int startPos = i * currentPartSize;
            RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
            currentPart.seek(startPos);
            threads[i] = new DownThread(startPos, currentPartSize,currentPart);
            threads[i].start();
        }
    }
    public double getCompleteRate(){
        int sumSize = 0;
        for(int i = 0; i < threadNum; i++){
            sumSize += threads[i].length;
        }
        return sumSize * 1.0 / fileSize;
    }
    private class DownThread extends Thread{
        private int startPos;
        private int currentPartSize;
        private RandomAccessFile currentPart;

        public int length;
        public DownThread(int startPos, int currentPartSize, RandomAccessFile currentPart){
            this.startPos = startPos;
            this.currentPartSize = currentPartSize;
            this.currentPart = currentPart;
        }

        @Override
        public void run() {
            try{
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5 * 1000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Accept","*/*");
                conn.setRequestProperty("Charset","UTF-8");
                InputStream inStream = conn.getInputStream();
                inStream.skip(this.startPos);
                byte[] buffer = new byte[1024];
                int hasRead = 0;
                while(length < currentPartSize && (hasRead = inStream.read(buffer)) != -1){
                    currentPart.write(buffer, 0, hasRead);
                    length += hasRead;
                }
                currentPart.close();
                inStream.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
